
package com.yiren.backstage.communication.analysis;

import android.content.Context;

import com.yiren.backstage.communication.services.IAnalysisStateService;
import com.yiren.backstage.communication.services.IGearService;
import com.yiren.backstage.communication.services.impl.AnalysisStateServiceImpl;
import com.yiren.backstage.communication.services.impl.GearServerImpl;
import com.yiren.backstage.communication.utils.BytesUtils;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.enums.GearTypeEnum;
import com.yiren.backstage.config.enums.SpeedTypeEnum;
import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.enums.CarStateNameEnum;
import com.yiren.backstage.currency.enums.CarStateValueEnum;

import java.util.Map;

/**
 * Time:2021/11/19 15:56
 * Author:xiaozan
 * Description:接线数据帧解析
 */
public class JxDataAnalysis {

    private Context context;
    private Map<String, Object> carSignalMap;
    private IAnalysisStateService iAnalysisStateService;
    private IGearService iGearService;
    private CarStateBean carState = CarStateBean.getInstance();
    private CarStateNameEnum valueMap;

    public JxDataAnalysis(Context context) {
        this.context = context;
        iAnalysisStateService = new AnalysisStateServiceImpl(context);
        iGearService = new GearServerImpl(context);
    }

    public void dataAnalysis(String data, Map<String, String> wiringMap) {
        // 防止数据解析异常给的try,catch
        try {
            String[] carDataArray = data.split(";");
            if (!data.contains("JX") || carDataArray.length < 12) {
                return;
            }
            carSignalMap = LoadConfigApi.loadCarSignalConfig(context).getMap();

            if (data.contains("$<JX")) SignalGConstant.MODE_COMMAND = !data.contains("$<JXST");

            // PORT0包含:远光灯信号、近光灯信号、后雾灯信号、示宽灯信号、左转向灯信号、右转向灯信号、脚杀制动器信号、喇叭信号
            byte[] bytes0 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[5]));
            // PORT1包含:离合器信号、安全带信号、主驾车门信号、手刹制动器信号、倒挡信号采集线、绕车左前、绕车左后、绕车右前
            byte[] bytes1 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[6]));
            // PORT2包含:绕车右后、预留位1、预留位2、预留位3、预留位4、预留位5、预留位6、预留位7
            byte[] bytes2 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[7]));
            // PORT2包含:内视镜、座椅、反光镜、指纹信号、档位传感器信号1、档位传感器信号2、档位传感器信号3、档位传感器信号4
            byte[] bytes3 = BytesUtils.int2bytes(Integer.parseInt(carDataArray[8]));
            boolean isMP = LoadConfigApi.loadSignalSourceConfig(context).isMpDevice();
            if (isMP) {
                //免破车型OBD获取不到的信号
                setSignal(wiringMap, "灰线", bytes0[0]);
                setSignal(wiringMap, "棕线", bytes0[1]);
                setSignal(wiringMap, "绿线", bytes0[4]);
                setSignal(wiringMap, "黄线", bytes0[5]);
                setSignal(wiringMap, "紫线", bytes0[6]);
                setSignal(wiringMap, "蓝线", bytes0[7]);
            } else {
                //远光
                carState.setDistanceLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DISTANCE_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.DISTANCE_LIGHT.toString()), bytes0[0]));
                //近光
                carState.setPassingLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.PASSING_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.PASSING_LIGHT.toString()), bytes0[1]));
                //左转
                carState.setLeftLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.LEFT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.LEFT_LIGHT.toString()), bytes0[4]));
                //右转
                carState.setRightLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.RIGHT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.RIGHT_LIGHT.toString()), bytes0[5]));
                // 警示灯
                if (carState.getLeftLight() == CarStateValueEnum.OPEN && carState.getRightLight() == CarStateValueEnum.OPEN) {
                    carState.setCautionLight(CarStateValueEnum.OPEN);
                } else {
                    carState.setCautionLight(CarStateValueEnum.CLOSE);
                }
                //脚刹
                carState.setFootBrake(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.FOOT_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.FOOT_BRAKE.toString()), bytes0[6]));
                //喇叭
                carState.setHorn(iAnalysisStateService.openAndCloseState(CarStateNameEnum.HORN, (Boolean) carSignalMap.get(CarStateNameEnum.HORN.toString()), bytes0[7]));

                //车速 车速获取来源是接线
                if (LoadConfigApi.loadSignalSourceConfig(context).getSpeedType() == SpeedTypeEnum.JX) {
                    carState.setSpeed(iAnalysisStateService.jxSpeed(carDataArray[1], (float) carSignalMap.get(CarStateNameEnum.SPEED.toString()), carDataArray[3]));
                    carState.setSpeedPulse(Float.parseFloat(carDataArray[3]));
                }
                //转速
                carState.setRotateSpeedPulse(Float.parseFloat(carDataArray[4]));
                carState.setRotateSpeed(iAnalysisStateService.jxRotateSpeed(carDataArray[2], (float) carSignalMap.get(CarStateNameEnum.ROTATE_SPEED.toString()), carDataArray[4]));
                // 雾灯
                carState.setFoglight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.FOGLIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.FOGLIGHT.toString()), bytes0[2]));
                // 行车灯
                carState.setDrivingLamp(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DRIVING_LAMP, (Boolean) carSignalMap.get(CarStateNameEnum.DRIVING_LAMP.toString()), bytes0[3]));
                // 离合
                carState.setSeparationReunion(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.SEPARATION_REUNION, (Boolean) carSignalMap.get(CarStateNameEnum.SEPARATION_REUNION.toString()), bytes1[0]));
                // 安全带
                carState.setSafetyBelt(iAnalysisStateService.hitchAndLoosenState(CarStateNameEnum.SAFETY_BELT, (Boolean) carSignalMap.get(CarStateNameEnum.SAFETY_BELT.toString()), bytes1[1]));
                // 车门
                carState.setCarDoor(iAnalysisStateService.openAndCloseState(CarStateNameEnum.CAR_DOOR, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_DOOR.toString()), bytes1[2]));
                // 手刹
                carState.setParkingBrake(iAnalysisStateService.loosenAndCockingState(CarStateNameEnum.PARKING_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.PARKING_BRAKE.toString()), bytes1[3]));
            }

            /***************************接线、免破通用解析信号************************************/

            // 倒档(0x10):
            if (bytes1[4] == 1) {
                carState.setGear(CarStateValueEnum.REVERSE_GEAR);
            } else {
                carState.setGear(CarStateValueEnum.NEUTRAL_GEAR);
            }
            //档位
            if (LoadConfigApi.loadSignalSourceConfig(context).getGearType() == GearTypeEnum.WHOLE_GEAR) {
                carState.setGear(iGearService.fullGear(bytes3));
            } else if (LoadConfigApi.loadSignalSourceConfig(context).getGearType() == GearTypeEnum.NULL_GEAR) {
                carState.setGear(iGearService.neutralGear(bytes3));
            } else {
                carState.setGear(iGearService.getGyroGears(context));
            }

            //左前
            carState.setCarHeadLeft(iAnalysisStateService.passAndNoPassState(CarStateNameEnum.CAR_HEAD_LEFT, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_HEAD_LEFT.toString()), bytes1[5]));
            // 左后
            carState.setCarTailLeft(iAnalysisStateService.passAndNoPassState(CarStateNameEnum.CAR_TAIL_LEFT, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_TAIL_LEFT.toString()), bytes1[6]));
            // 右前(0x40)
            carState.setCarHeadRight(iAnalysisStateService.passAndNoPassState(CarStateNameEnum.CAR_HEAD_RIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_HEAD_RIGHT.toString()), bytes1[7]));
            // 右后
            carState.setCarTailRight(iAnalysisStateService.passAndNoPassState(CarStateNameEnum.CAR_TAIL_RIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_TAIL_RIGHT.toString()), bytes2[0]));
            if (isMP) {
                // 配置7
                setSignal(wiringMap, "橙线", bytes2[1]);
                // 配置8
                setSignal(wiringMap, "粉线", bytes2[2]);
                // 配置9
                setSignal(wiringMap, "白线", bytes2[3]);
                // 配置10
                setSignal(wiringMap, "黑线", bytes2[4]);
            } else {
                carState.setCarKey(iAnalysisStateService.carKeyState(CarStateNameEnum.CAR_KEY, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_KEY.toString()), bytes2[1]));
                carState.setRainWipper(iAnalysisStateService.corneringLamp(CarStateNameEnum.RAIN_WIPPER, (Boolean) carSignalMap.get(CarStateNameEnum.RAIN_WIPPER.toString()), bytes2[2]));
            }

            if (isMP) {
                String jxCarStateConfig = LoadConfigApi.loadDeviceStudyConfig(context).getJxConfig();
                // 警示灯
                if (jxCarStateConfig.contains(CarStateNameEnum.LEFT_LIGHT.toString()) && jxCarStateConfig.contains(CarStateNameEnum.RIGHT_LIGHT.toString())) {
                    // 警示灯
                    if (carState.getLeftLight() == CarStateValueEnum.OPEN && carState.getRightLight() == CarStateValueEnum.OPEN) {
                        carState.setCautionLight(CarStateValueEnum.OPEN);
                    } else {
                        carState.setCautionLight(CarStateValueEnum.CLOSE);
                    }
                }
            }

            // 后视镜(0x20)
            carState.setRearviewMirror(iAnalysisStateService.adjustAndNoAdjustState(CarStateNameEnum.REARVIEW_MIRROR, (Boolean) carSignalMap.get(CarStateNameEnum.REARVIEW_MIRROR.toString()), bytes3[0]));
            // 座椅(0x40)
            carState.setSeat(iAnalysisStateService.adjustAndNoAdjustState(CarStateNameEnum.SEAT, (Boolean) carSignalMap.get(CarStateNameEnum.SEAT.toString()), bytes3[1]));
            // 反光镜(0x80)
            carState.setReflectorMirror(iAnalysisStateService.adjustAndNoAdjustState(CarStateNameEnum.REFLECTOR_MIRROR, (Boolean) carSignalMap.get(CarStateNameEnum.REFLECTOR_MIRROR.toString()), bytes3[2]));
            // 车内-指纹仪(0x04)
            carState.setFingerprint(iAnalysisStateService.adjustAndNoAdjustState(CarStateNameEnum.FINGERPRINT_INSTRUMENT, (Boolean) carSignalMap.get(CarStateNameEnum.FINGERPRINT_INSTRUMENT.toString()), bytes3[3]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setSignal(Map<String, String> wiringMap, String lineColor, int bytes) {
        if (null != wiringMap && null != wiringMap.get(lineColor)) {
            valueMap = CarStateNameEnum.valueOf(wiringMap.get(lineColor));
            if (null == valueMap) {
                return;
            }
            switch (valueMap) {
                case DISTANCE_LIGHT:
                    carState.setDistanceLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DISTANCE_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.DISTANCE_LIGHT.toString()), bytes));
                    break;
                case PASSING_LIGHT:
                    carState.setPassingLight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.PASSING_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.PASSING_LIGHT.toString()), bytes));
                    break;
                case FOGLIGHT:
                    carState.setFoglight(iAnalysisStateService.openAndCloseState(CarStateNameEnum.FOGLIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.FOGLIGHT.toString()), bytes));
                    break;
                case DRIVING_LAMP:
                    carState.setDrivingLamp(iAnalysisStateService.openAndCloseState(CarStateNameEnum.DRIVING_LAMP, (Boolean) carSignalMap.get(CarStateNameEnum.DRIVING_LAMP.toString()), bytes));
                    break;
                case LEFT_LIGHT:
                    carState.setLeftLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.LEFT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.LEFT_LIGHT.toString()), bytes));
                    break;
                case RIGHT_LIGHT:
                    carState.setRightLight(iAnalysisStateService.corneringLamp(CarStateNameEnum.RIGHT_LIGHT, (Boolean) carSignalMap.get(CarStateNameEnum.RIGHT_LIGHT.toString()), bytes));
                    break;
                case HORN:
                    carState.setHorn(iAnalysisStateService.openAndCloseState(CarStateNameEnum.HORN, (Boolean) carSignalMap.get(CarStateNameEnum.HORN.toString()), bytes));
                    break;
                case CAR_DOOR:
                    carState.setCarDoor(iAnalysisStateService.openAndCloseState(CarStateNameEnum.CAR_DOOR, (Boolean) carSignalMap.get(CarStateNameEnum.CAR_DOOR.toString()), bytes));
                    break;
                case FOOT_BRAKE:
                    carState.setFootBrake(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.FOOT_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.FOOT_BRAKE.toString()), bytes));
                    break;
                case PARKING_BRAKE:
                    carState.setParkingBrake(iAnalysisStateService.loosenAndCockingState(CarStateNameEnum.PARKING_BRAKE, (Boolean) carSignalMap.get(CarStateNameEnum.PARKING_BRAKE.toString()), bytes));
                    break;
                case SEPARATION_REUNION:
                    carState.setSeparationReunion(iAnalysisStateService.brakeingAndLoosenState(CarStateNameEnum.SEPARATION_REUNION, (Boolean) carSignalMap.get(CarStateNameEnum.SEPARATION_REUNION.toString()), bytes));
                    break;
                case SAFETY_BELT:
                    carState.setSafetyBelt(iAnalysisStateService.hitchAndLoosenState(CarStateNameEnum.SAFETY_BELT, (Boolean) carSignalMap.get(CarStateNameEnum.SAFETY_BELT.toString()), bytes));
                    break;
            }
        }
    }
}

